<template>
  <div class="admin-dashboard">
    <!-- 顶部导航栏 -->
    <header class="admin-header">
      <div class="header-left">
        <h1 class="system-title">智能伙伴匹配系统后台管理</h1>
      </div>
      <div class="header-right">
        <div class="user-info">
          <span class="username">管理员：{{ username }}</span>
          <el-button type="text" @click="handleLogout">退出登录</el-button>
        </div>
      </div>
    </header>

    <!-- 主要内容区域 -->
    <div class="admin-main">
      <!-- 左侧菜单 -->
      <aside class="admin-sidebar">
        <el-menu
          v-model:default-active="activeMenu"
          class="admin-menu"
          mode="vertical"
          background-color="#fff"
          text-color="#303133"
          active-text-color="#409EFF"
          @select="handleMenuSelect"
          unique-opened
        >
          <el-menu-item index="users">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>

          <el-menu-item index="session-history">
            <el-icon><User /></el-icon>
            <span>会话历史管理</span>
          </el-menu-item>
          
          <el-menu-item index="sessions">
            <el-icon><Message /></el-icon>
            <span>房间会话管理</span>
          </el-menu-item>
          
          <el-menu-item index="messages">
            <el-icon><ChatDotRound /></el-icon>
            <span>聊天信息管理</span>
          </el-menu-item>
          
          <el-menu-item index="rooms">
            <el-icon><UserFilled /></el-icon>
            <span>房间/队伍管理</span>
          </el-menu-item>
          
          <el-menu-item index="analysis">
          <el-icon><PieChart /></el-icon>
          <span>数据分析</span>
        </el-menu-item>

        </el-menu>
      </aside>

      <!-- 右侧内容区 -->
      <main class="admin-content">
        <div class="content-wrapper">
          <router-view />
        </div>
      </main>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Message, ChatDotRound, UserFilled, PieChart } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const username = ref('')
const activeMenu = ref('users')

// 处理菜单选择
const handleMenuSelect = (index) => {
  activeMenu.value = index
  router.push(`/admin/${index}`)
}

// 退出登录
const handleLogout = () => {
  sessionStorage.removeItem('isLoggedIn')
  sessionStorage.removeItem('username')
  ElMessage.success('已退出登录')
  router.push('/login')
}

// 监听路由变化，更新当前激活的菜单
watch(
  () => route.path,
  (newPath) => {
    const pathSegments = newPath.split('/')
    if (pathSegments.length >= 3) {
      activeMenu.value = pathSegments[2]
    }
  }
)

// 页面加载时初始化
onMounted(() => {
  // 获取用户名
  const storedUsername = sessionStorage.getItem('username')
  if (storedUsername) {
    username.value = storedUsername
  }
  
  // 初始化激活菜单
  const pathSegments = route.path.split('/')
  if (pathSegments.length >= 3) {
    activeMenu.value = pathSegments[2]
  }
  
  // 添加淡入动画
  document.querySelector('.admin-content').classList.add('fade-in')
})
</script>

<style scoped>
.admin-dashboard {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
  overflow: hidden;
}

/* 顶部导航栏 */
.admin-header {
  background: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 0 24px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  z-index: 100;
}

.system-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 16px;
}

.username {
  color: #606266;
}

/* 主要内容区域 */
.admin-main {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* 左侧菜单 - 确保在PC端始终显示 */
.admin-sidebar {
  width: 200px;
  background: white;
  box-shadow: 2px 0 6px rgba(0, 0, 0, 0.1);
  overflow-y: auto;
  flex-shrink: 0;
}

.admin-menu {
  border-right: none;
  height: 100%;
  padding-top: 16px;
}

.admin-menu .el-menu-item {
  height: 60px;
  line-height: 60px;
  font-size: 15px;
}

.admin-menu .el-menu-item.is-active {
  background-color: #ecf5ff;
}

/* 右侧内容区 */
.admin-content {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  opacity: 0;
  transform: translateX(20px);
  transition: opacity 0.5s ease, transform 0.5s ease;
}

.fade-in {
  opacity: 1;
  transform: translateX(0);
}

.content-wrapper {
  background: white;
  border-radius: 8px;
  padding: 24px;
  min-height: calc(100vh - 160px);
}

/* 响应式设计 - 优先PC端，移动端适配 */
@media (max-width: 1024px) {
  .admin-sidebar {
    width: 180px;
  }
  
  .admin-menu .el-menu-item {
    height: 50px;
    line-height: 50px;
    font-size: 14px;
  }
  
  .admin-content {
    padding: 16px;
  }
}

@media (max-width: 768px) {
  .admin-main {
    flex-direction: column;
  }
  
  .admin-sidebar {
    width: 100%;
    height: 60px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }
  
  .admin-menu {
    display: flex;
    overflow-x: auto;
    padding-top: 0;
  }
  
  .admin-menu .el-menu-item {
    min-width: 120px;
    height: 60px;
    line-height: 60px;
  }
  
  .content-wrapper {
    padding: 16px;
    min-height: calc(100vh - 184px);
  }
}
</style>